package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.bean.ScheduleData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.CcMsgStructure;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.StringUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Megan on 16/3/14.
 */
public class ScheduleDetailActivity extends Activity implements View.OnClickListener {

    private TextView mTvTitle, mTvCreator, mTvAddress, mTvDate, mTvTime, mTvRemark, mTvEdit;
    private LinearLayout mLlBack;
    private ScheduleData scheduleData;
    private String mUid;
    private boolean isMe;

    public static ScheduleDetailActivity instance = null;
    private static final int CALENDAR_GET = 20001;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CALENDAR_GET:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_detail_layout);
        instance = this;
        initViews();
        getIntentData();
        calendarGet();
    }

    private void initViews() {
        mTvTitle = (TextView) this.findViewById(R.id.tv_schedule_detail_title);
        mTvEdit = (TextView) this.findViewById(R.id.tv_schedule_detail_edit);
        mTvCreator = (TextView) this.findViewById(R.id.tv_schedule_detail_creator);
        mTvAddress = (TextView) this.findViewById(R.id.tv_schedule_detail_address);
        mTvDate = (TextView) this.findViewById(R.id.tv_schedule_detail_date);
        mTvTime = (TextView) this.findViewById(R.id.tv_schedule_detail_time);
        mTvRemark = (TextView) this.findViewById(R.id.tv_schedule_detail_remark);
        mLlBack = (LinearLayout) this.findViewById(R.id.ll_schedule_detail_back);

        mTvEdit.setOnClickListener(this);
        mLlBack.setOnClickListener(this);
    }


    private void getIntentData() {
        Intent intent = getIntent();
        isMe = intent.getBooleanExtra("isMe", true);
        scheduleData = (ScheduleData) intent.getSerializableExtra("scheduleData");
        mTvTitle.setText(scheduleData.getTitle());
        mTvCreator.setText("发起人：" + scheduleData.getPublishedUName());
        if (StringUtil.isEmpty(scheduleData.getAddress())) {
            mTvAddress.setVisibility(View.GONE);
        } else {
            mTvAddress.setVisibility(View.VISIBLE);
            mTvAddress.setText(scheduleData.getAddress());
        }
        if (StringUtil.isEmpty(scheduleData.getRemark())) {
            mTvRemark.setText("暂无备注");
        } else {
            mTvRemark.setText(scheduleData.getRemark());
        }
        mTvDate.setText(DateUtil.getYearDate1(Long.parseLong(scheduleData.getStartTime()) * 1000));
        if (!"0".equals(scheduleData.getEndTime())) {
            mTvTime.setText(DateUtil.getHourAndMin(Long.parseLong(scheduleData.getStartTime()) * 1000) + "-" + DateUtil.getHourAndMin(Long.parseLong(scheduleData.getEndTime()) * 1000));
        } else {
            mTvTime.setText(DateUtil.getHourAndMin(Long.parseLong(scheduleData.getStartTime()) * 1000) + "-");
        }
        mUid = intent.getStringExtra("userId");
    }

    /**
     * 日程请求 （消除红点）
     */
    private void calendarGet() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_ID, scheduleData.getId()));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_TYPE, "1"));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_CALENDAR_BOSS_UID, mUid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));

        CcMsgStructure msg = new CcMsgStructure() {
            @Override
            public void onReceive(JSONObject data) {

            }

            @Override
            public void onError(int error, Object ret) {

            }
        };
        msg.mApi = AsynHttpClient.API_EVENT_CALENDAR_GET;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_schedule_detail_back:
                finish();
                break;
            case R.id.tv_schedule_detail_edit:
                if ((!isMe) && "1".equals(scheduleData.getDone())) {
                    Toast.makeText(this, "该日程已完成,无法再进行编辑", Toast.LENGTH_LONG).show();
                } else {
                    Intent intent = new Intent(ScheduleDetailActivity.this, ScheduleCreateActivity.class);
                    intent.putExtra("isMe", isMe);
                    intent.putExtra("scheduleData", scheduleData);
                    intent.setAction("editSchedule");
                    startActivity(intent);
                }
                break;
        }
    }
}
